// 5 clicked buttons :up down go pause stop //<>//

// === up down === //
class cclickButton {
  cclickButton(int x, int y, String text, handler action) {
    this.x = x;
    this.y = y;
    this.text = text;
    this.action = action;
    colr = #F09B2C;
    enabled = true;
  }

  void checkMouseClick(int mx, int my) {
    if (enabled) {
      if (checkRange(mx, my)) {
        action.exec(this);
      }
    }
  }

  void paint() {
    fill(colr);
    textSize(30);
    text(text, x, y);
  }

  // not in range, return false. in range, return true
  protected boolean checkRange(int mx, int my) {
    if (mx < x || my > y) {
      return false;
    }
    if (mx > (x + XRANGE) || my < (y - YRANGE)) {
      return false;
    }
    // mx, my in range, button is clicked
    setMouseClickCirlce(mx, my);
    return true;
  }

  void enable() {
    enabled = true;
  }

  void disable() {
    enabled = false;
  }

  protected handler action;
  protected int x, y;
  protected String text;
  protected color colr;
  protected boolean enabled;

  SoundFile sound;
};

// button click detect range
final int XRANGE = 120;
final int YRANGE = 40;

// === go === //
class cbuttonGo extends cclickButton {
  cbuttonGo(int x, int y, String text, handler action) {
    super(x, y, text, action);
  }

  void paint() {
    textSize(80);
    if (enabled) {
      fill(colr);
    } else {
      fill(colr, 60);
    }
    text(text, x, y);
  }
};

// === pause === //
class cbuttonPause extends cclickButton {
  cbuttonPause(int x, int y, String text, handler action) {
    super(x, y, text, action);
    colr = #48A719; // green
    paused = false;
    invisible();
    blnk = 0;
  }

  void visible() {
    // if set visible, then must set enble
    visibled = true;
    enabled = true;
  }

  void invisible() {
    // if set invisible, then must set disable
    visibled = false;
    enabled = false;
  }

  void paint() {
    if (visibled) {
      textSize(55);
      if (paused) {
        blnk++;
        if (blnk < 20) {
          fill(#F57474); // red
          text("paused", x, y);
        } else if (blnk < 40) {
          // no action
        } else {
          blnk = 0;
        }
      } else {
        fill(colr);
        text(text, x, y);
      }
    }
  }

  int blnk; // blink
  private boolean visibled;
  boolean paused;
};

// === stop === //
class cbuttonStop extends cclickButton {
  cbuttonStop(int x, int y, handler action) {
    super(x, y, "", action); // image button, no text
    invisible();
  }

  void visible() {
    // if set visible, then must set enble
    visibled = true;
    enabled = true;
  }

  void invisible() {
    // if set invisible, then must set disable
    visibled = false;
    enabled = false;
  }

  void paint() { // this is an image button, no text
    if (visibled) {
      set(x - 43, y - 86, imgStop); // fined tuned offset for this image position
    }
  }

  PImage img;
  private boolean visibled;
};

// all buttons
cclickButton btnUp = new cclickButton(1490, 54, "up", (cclickButton btn)->ButtonUp(btn));
cclickButton btnDn = new cclickButton(1490, 111, "down", (cclickButton btn)->ButtonDn(btn));
cbuttonGo btnGo = new cbuttonGo(1600, 80, "go", (cclickButton btn)->ButtonGo(btn));
cbuttonPause btnPause = new  cbuttonPause(1605, 520, "pause", (cclickButton btn)->ButtonPause(btn));
cbuttonStop btnStop = new cbuttonStop(1683, 826, (cclickButton btn)->ButtonStop(btn)); // image button, no text

// array of all buttons
cclickButton[] clickBtns = {btnUp, btnDn, btnGo, btnPause, btnStop};

void drawallBtns() {
  for (cclickButton i : clickBtns) {
    i.paint();
  }
}

void checkMouseClick(int mx, int my) {
  for (cclickButton i : clickBtns) {
    i.checkMouseClick(mx, my);
  }
}

void fullStop() {
  for (cclickButton i : clickBtns) {
    i.enabled = true; // up, down, go, pause, stop enable
  }
  // but
  btnPause.invisible(); // pause invisible (and will be disable of course)
  btnStop.invisible(); // stop invisible (and will be disable of course)

  stopWatch.stop();
  cursor(ARROW);

  playSound(sndDone);
  stateMachine = 100;
}

void allButtonEnable() {
  for (cclickButton i : clickBtns) {
    i.enable();
  }
}

void allButtonDisable() {
  for (cclickButton i : clickBtns) {
    i.disable();
  }
}

// ============ button handlers ===============  //
void ButtonUp(cclickButton btn) {
  if (numPlates < 10) {
    preparePlates(++numPlates);
    playSound(btn.sound);
    println("number of plates = " + numPlates);
  }
}

void ButtonDn(cclickButton btn) {
  if (numPlates > 3) {
    preparePlates(--numPlates);
    playSound(btn.sound);
    println("number of plates = " + numPlates);
  }
}

void ButtonGo(cclickButton btn) {
  preparePlates(numPlates);
  allButtonDisable();
  btnPause.visible(); // pause visible (and will be enable of course)
  btnStop.visible(); // stop visible (and will be enable of course)
  btnPause.paused = false;
  stopWatch.restart();
  playSound(btn.sound);
  stateMachine = 1; // trigger to drag stage machine
}


void ButtonPause(cclickButton btn) {
  cbuttonPause btn_ = (cbuttonPause)btn;
  btn_.paused = !btn_.paused;
  if (btn_.paused) {
    playSound(btn_.sound);
    stopWatch.pause();
    cursor(WAIT);
  } else {
    stopWatch.resume();
    playSound(btn_.sound);
    cursor(ARROW);
  }
}

void ButtonStop(cclickButton btn) {
  fullStop();
}
